@using System.Web.Http
@using System.Web.Http.Controllers
@using System.Web.Http.Description
@using WebFoundation.WebServer.Areas.HelpPage
@using WebFoundation.WebServer.Areas.HelpPage.Models
@model IGrouping<HttpControllerDescriptor, ApiDescription>

@{
    var controllerDocumentation = ViewBag.DocumentationProvider != null ?
        ViewBag.DocumentationProvider.GetDocumentation(Model.Key) :
        null;
}

<h2 id="@Model.Key.ControllerName">@Model.Key.ControllerName</h2>
@if (!String.IsNullOrEmpty(controllerDocumentation))
{
    <p>@controllerDocumentation</p>
}
<table class="help-page-table">
    <thead>
        <tr><th>API</th><th>Description</th><th>Roles</th></tr>
    </thead>
    <tbody>
        @foreach (var api in Model)
        {
            <tr>
                <td class="api-name"><a href="@Url.Action("Api", "Help", new { apiId = api.GetFriendlyId() })">@api.HttpMethod.Method @api.RelativePath</a></td>
                <td class="api-documentation">
                    @if (api.Documentation != null)
                    {
                        <p>@api.Documentation</p>
                    }
                    else
                    {
                        <p>No documentation available.</p>
                    }
                </td>
                <td class="api-roles">

                    @if (api.ActionDescriptor.GetCustomAttributes<WebFoundation.RolesAttribute>().Count > 0 || api.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<WebFoundation.RolesAttribute>().Count > 0)
                    {
                        var controlRoles = api.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<WebFoundation.RolesAttribute>().Select(r => r.Roles);
                        var roles = api.ActionDescriptor.GetCustomAttributes<WebFoundation.RolesAttribute>().Select(r => r.Roles);
                        roles = controlRoles.Union(roles);
                        <p>@string.Join(",", roles) </p>
                    }
                    else
                    {
                        <p>No role.</p>
                    }
                </td>
            </tr>
        }
    </tbody>
</table>